package com.wangjiacai.hqlQuery.role.security.service;

import com.wangjiacai.hqlQuery.role.service.TokenUserService;
import com.wangjiacai.hqlQuery.role.security.authentication.TokenAuthentication;
import com.wangjiacai.utils.web.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnBean(RedisService.class)
public class TokenAuthenticationUserService implements TokenUserService {

    @Autowired
    private RedisService redisService;

    @Override
    public String findUserId(String token) {
        TokenAuthentication tokenAuthentication=redisService.get(token,TokenAuthentication.class);
        if(tokenAuthentication==null) return null;
        return (String)tokenAuthentication.getCredentials();
    }

    @Override
    public <T> T findUserCredentials(String token, Class<T> returnType) {
        return redisService.get(token,returnType);
    }
}
